package com.doujiang.Controller;

import com.doujiang.domain.Rating;
import com.doujiang.service.IRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ratings")
@CrossOrigin
public class RatingController {

    @Autowired
    private IRatingService ratingService;

    /**
     * 根据用户ID获取用户的所有评分
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getUserRatings(@PathVariable("userId") Long userId) {
        List<Rating> ratings = ratingService.getRatingsByUserId(userId);
        return ResponseEntity.ok(ratings);
    }

    /**
     * 删除评分
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteRating(@PathVariable("id") Long id) {
        boolean success = ratingService.removeById(id);
        if (success) {
            return ResponseEntity.ok("评分删除成功");
        } else {
            return ResponseEntity.status(500).body("评分删除失败");
        }
    }

    /**
     * 添加评分
     */
    @PostMapping
    public ResponseEntity<?> addRating(@RequestBody Rating rating) {
        boolean success = ratingService.save(rating);
        if (success) {
            return ResponseEntity.ok(rating);
        } else {
            return ResponseEntity.status(500).body("评分添加失败");
        }
    }
} 